﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DTO;
using BUS;

namespace TVC_WEB.Global
{
    public partial class NewsDetail : System.Web.UI.Page
    {
        NewsControl newsctr = new NewsControl();
        NewsInfo newsinfo = new NewsInfo();
        string sErr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((Request["newid"] != "" && Request["newid"] != null) || Session["newid"] != null)
            {
                if (Session["newid"] != null)
                {
                    newsinfo = newsctr.Get(Convert.ToInt64(Session["newid"]), ref sErr);
                }
                else
                    newsinfo = newsctr.Get(Convert.ToInt64(Request["newid"]), ref sErr);
                NewsCategoriesControl newcatectr = new NewsCategoriesControl();
                NewsCategoriesInfo newcateinfo = new NewsCategoriesInfo();
                if (newcatectr.IsExist(newsinfo.IDNewsCategory))
                {
                    newcateinfo = newcatectr.Get(newsinfo.IDNewsCategory, ref sErr);

                    lbCategoryNew.Text = newcateinfo.NameVN;
                    getNewDetail(newsinfo.ID);
                    getNewCategory(newcateinfo.ID, newsinfo.ID);
                }
            }
        }
        public void getNewDetail(long iID)
        {
            newsinfo = newsctr.Get(iID, ref sErr);
            DataTable dt = newsinfo.ToDataTable();
            dt.Rows.Add(newsinfo.ToDataRow(dt));
            listdetailnew.DataSource = dt;
            listdetailnew.DataBind();
        }
        public void getNewCategory(int icategoryid, long iID)
        {
            DataTable dt = newsctr.GetAll_News_IDcategory(icategoryid, iID, ref sErr);

            DataTable kq = newsinfo.ToDataTable();
            for (int i = dt.Rows.Count - 1; i >= dt.Rows.Count - 4; i--)
            {
                if (i > -1)
                {

                    NewsInfo temp = new NewsInfo(dt.Rows[i]);

                    kq.Rows.Add(temp.ToDataRow(kq));
                }
            }
            listListNews.DataSource = kq;
            listListNews.DataBind();
        }
        public string Formatdatepost(String date, String IDcate)
        {


            String[] s = date.Split('/');
            String kq = "";
            if (s.Length > 0)
            {
                kq = s[1] + "/" + s[0] + "/" + s[2];
            }
            else
                kq = "";
            return "Ngày đăng : " + kq;

        }
        public string FormatDate(String date)
        {
            String[] s = date.Split('/');
            if (s.Length > 0)
            {
                return s[1] + "/" + s[0] + "/" + s[2];
            }
            return "";
        }
    }
}
